/*注册页面*/
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frametrim/app/app_navigator.dart';
import 'package:frametrim/utility/screen.dart';
import 'package:frametrim/utility/sq_color.dart';
import 'package:frametrim/views/button_circle.dart';
import 'package:frametrim/views/input_background_view.dart';
import 'package:frametrim/views/textfield_phone.dart';
import 'package:frametrim/views/textfield_psw.dart';
import 'package:frametrim/views/textfield_view.dart';
import 'package:frametrim/views/textfiled_num.dart';

import 'certificationPage.dart';

class RegisterPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RegisterPageState();
}

class RegisterPageState extends State<RegisterPage> {
  TextEditingController _phoneController = TextEditingController();
  TextEditingController _verificationController = TextEditingController();
  TextEditingController _pswController = TextEditingController();
  late Timer _counddownTimer;
  int _countdownNum = 60; //倒计时
  bool isCanPress = true;

  @override
  void initState() {
    super.initState();
    _counddownTimer = Timer.periodic(Duration(seconds: 1), (timer) {});
  }

  @override
  void dispose() {
    _counddownTimer.cancel(); //销毁计时器
    // _counddownTimer=null;
    _phoneController.dispose();
    _verificationController.dispose();
    _pswController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: <Widget>[
          Image.asset(
            'image/background.png',
            fit: BoxFit.cover,
            width: Screen.width,
            height: Screen.height,
          ),
          Container(
            width: Screen.width,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(
                  height: 90 + Screen.topSafeHeight,
                ),
                Text(
                  '账号注册',
                  style: TextStyle(fontSize: 23, color: SQColor.black),
                ),
                SizedBox(
                  height: 70,
                ),
                buildInputViews(),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /*输入框*/
  Widget buildInputViews() {
    return Column(
      children: <Widget>[
        InputBackGroundView(
            2,
            TextFieldView(
                inputStyle: "LEFT_TEXT",
                inputType: "PHONE",
                hiteText: '请输入手机号',
                controller: _phoneController)),
        SizedBox(height: 15),
        Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(25),
            color: SQColor.white,
          ),
          height: 45,
          margin: EdgeInsets.fromLTRB(28, 0, 28, 0),
          padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: TextFieldView(
                    inputType: "YZM",
                    hiteText: '请输入验证码',
                    controller: _verificationController),
              ),
              FlatButton(
                //按钮禁用时候的颜色
                disabledColor: SQColor.white,
                //按钮禁用时文字颜色
                disabledTextColor: SQColor.gray,
                textColor: SQColor.color_3C64F9,
                //splashColor+highlightColor去掉点击水波纹效果
                splashColor: SQColor.trans,
                highlightColor: SQColor.trans,
                child: Text(
                  isCanPress ? '发送验证码' : '${_countdownNum}秒后重发',
                  style: TextStyle(
                      fontSize: 16,
                      color: isCanPress ? SQColor.color_3C64F9 : SQColor.gray),
                ),
                onPressed: () {
                  sendVerfication();
                },
              ),
            ],
          ),
        ),
        SizedBox(height: 15),
        InputBackGroundView(
            2,
            TextFieldView(
                inputStyle: "NONE",
                inputType: "PSW",
                imageLeft: Image.asset(
                  'image/icon_acc_psd02.png',
                  width: 28,
                  height: 28,
                ),
                openEyes: Image.asset(
                  'image/psd_eye_on.png',
                  width: 24,
                  height: 24,
                ),
                closeEyes: Image.asset(
                  'image/psd_eye_off.png',
                  width: 24,
                  height: 24,
                ),
                hiteText: '设置登录密码',
                controller: _pswController)),
        SizedBox(
          height: 28,
        ),
        ButtonCircleView(
            '提交注册', SQColor.white, SQColor.color_3C64F9, submitRegister)
      ],
    );
  }

  /*提交注册*/
  submitRegister() {
    print('点击提交注册');
    AppNavigator.push(context, CertificationPage());
  }

  /*发送验证码*/
  void sendVerfication() {
    setState(() {
      if (isCanPress == true) {
        isCanPress = false;
        _initTimer();
        return null; //返回null 按钮禁止点击
      } else {
        return null;
      }
    });
  }

/*倒计时*/
  void _initTimer() {
    _counddownTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      _countdownNum--;
      setState(() {
        if (_countdownNum == 0) {
          _counddownTimer.cancel();
          _countdownNum = 60;
          isCanPress = true;
        } else {
          isCanPress = false;
        }
      });
    });
  }
}
